package com.ymatou.iapi.app.keyword.testcase;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.app.keyword.parameter.HotSellerByTopicBean;
import com.ymatou.iapi.app.keyword.service.HotSellerByTopicCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;



public class Ts_HotSellerByTopic {
	private static HotSellerByTopicBean hotSellerBean;
	private static HotSellerByTopicCall hotSellerCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("热门主题买手聚合页接口");
	}

	@Before
	public void caseUp() {
		hotSellerBean=new HotSellerByTopicBean();
		hotSellerCall=new HotSellerByTopicCall();
		
	}

	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(hotSellerCall.getOpurl());
		
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_HotSellerByTopic_001() {
		Logger.start(true,"热门主题买手聚合页 ,验证接口返回公共数据 ");
		try {
			//设置参数
			int fparentcategoryarrayids = 1086;
			hotSellerBean.setFilter("term(fparentcategoryarrayids,"+fparentcategoryarrayids+")");
			hotSellerBean.setExplain("False");
			hotSellerBean.setInfo("yid:111,userid:111,cookieid:111555555555");
			hotSellerBean.setLimit("0,10");
			
			hotSellerCall.setData(hotSellerBean);
			hotSellerCall.callService();			
			
			JSONArray hits = hotSellerCall.getHits();
			
			Logger.verifyEquals("OK",hotSellerCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(hotSellerCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, hotSellerCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, hotSellerCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			Logger.verifyNotNull(hotSellerCall.getCommonInfo("hostip"), "服务器ip不为空");
			Logger.verifyNotNull(hotSellerCall.getCommonInfo("requestid"), "requestid不为空");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_HotSellerByTopic_002() {
		Logger.start(true,"热门主题买手聚合页- 验证接口返回的买手是否重复 ");
		try {
			// 设置参数
			int fparentcategoryarrayids = 1086;
			hotSellerBean.setFilter("term(fparentcategoryarrayids,"+fparentcategoryarrayids+")");
			hotSellerBean.setExplain("False");
			hotSellerBean.setInfo("yid:111,userid:111,cookieid:111555555555");
			hotSellerBean.setLimit("0,10");
			
			hotSellerCall.setData(hotSellerBean);
			hotSellerCall.callService();
			
			JSONArray hitsAry = hotSellerCall.getHits();
			
			List<String> list = new ArrayList<String>();

			for (int i = 1; i <= hitsAry.length(); i++) {
				
				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				String iuserid = json.getString("iuserid");
				
				if (list.contains(iuserid) == true) {

					Logger.verifyEquals(false, list.contains(iuserid), "验证第" + i+ "条返回数据的noteid存在重复值" + iuserid);

				}else{
					Logger.verifyEquals(false, list.contains(iuserid), "验证第" + i+ "条返回数据的noteid不存在重复值" + iuserid);
					list.add(iuserid);
				}
			
			}
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_HotSellerByTopic_003() {
		Logger.start(true,"热门主题买手聚合页 ,验证接口返回是否按照买手商品数排序 ");
		try {
			//设置参数
			int fparentcategoryarrayids = 1086;
			hotSellerBean.setFilter("term(fparentcategoryarrayids,"+fparentcategoryarrayids+")");
			hotSellerBean.setExplain("False");
			hotSellerBean.setInfo("yid:111,userid:111,cookieid:111555555555");
			hotSellerBean.setLimit("0,10");
			
			hotSellerCall.setData(hotSellerBean);
			hotSellerCall.callService();			
			
			for (int i = 1; i <=hotSellerCall.getHits().length(); i++) {
				for (int j = i+1; j <= hotSellerCall.getHits().length(); j++) {
					
					String noteInfo1 = hotSellerCall.getHits().get(i - 1).toString();
					JSONObject noteInfoJson1 = new JSONObject(noteInfo1);
					int popularity1 =  noteInfoJson1.getInt("count");
					
					String noteInfo2 = hotSellerCall.getHits().get(j - 1).toString();
					JSONObject noteInfoJson2 = new JSONObject(noteInfo2);
					int popularity2=  noteInfoJson2.getInt("count");
					
					Logger.verifyEquals(true, popularity1>0, "买手商品数量大于0");
					Logger.verifyEquals(true, popularity1>=popularity2, "检查排在第"+i+"位的标签热度值为"+popularity1+"的笔记比排在第"+j+"位的标签热度值为:"+popularity2+"笔记热度值高");
				}
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_HotSellerByTopic_004() {
		Logger.start(false,"热门主题买手聚合页 ,传入不存在的前端类目id，检查接口返回状态及数据结果 ");
		try {
			//设置参数
			int fparentcategoryarrayids = 1212121111;
			hotSellerBean.setFilter("term(fparentcategoryarrayids,"+fparentcategoryarrayids+")");
			hotSellerBean.setExplain("False");
			hotSellerBean.setInfo("yid:111,userid:111,cookieid:111555555555");
			hotSellerBean.setLimit("0,10");
			
			hotSellerCall.setData(hotSellerBean);
			hotSellerCall.callService();			
			
			Logger.verifyEquals("OK", hotSellerCall.getStatus(), "验证接口返回状态是否正常");
			Logger.verifyEquals(true, hotSellerCall.getHits().length()<=2, "接口没有返回数据");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	//TODO 后期还得加去数据库关联fparentcategoryarrayids统计用户商品数的case
}
